package io.renren.modules.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.back.entity.CategoryEntity;
import io.renren.modules.back.service.CategoryService;
import io.renren.modules.oss.service.SysOssService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品分类接口
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年1月26日 上午10:40:10
 */
@RestController
@RequestMapping("/back/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SysOssService sysOssService;

    /**
     * 查看列表
     */
    @GetMapping("/list")
    @RequiresPermissions("category:list")
    public R list(){
        List<CategoryEntity> categoryList = categoryService.list();
        return R.ok().put("data", categoryList);
    }

    /**
     * 条件查询
     */
    @GetMapping("/select")
    @RequiresPermissions("category:list")
    public R select(@RequestParam Map<String, Object> params){
        List<CategoryEntity> categoryList = categoryService.list(
                new QueryWrapper<CategoryEntity>().eq("parent_id",0)
        );
        return R.ok().put("data", categoryList);
    }

    /**
     * 查看信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("category:info")
    public R info(@PathVariable("id") Integer id){
        CategoryEntity categoryEntity = categoryService.getById(id);
        return R.ok().put("data", categoryEntity);
    }

    /**
     * 保存
     */
    @SysLog("保存商品分类")
    @PostMapping("/save")
    @RequiresPermissions("category:save")
    public R save(@RequestBody CategoryEntity categoryEntity){
        ValidatorUtils.validateEntity(categoryEntity);
        categoryService.save(categoryEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改商品分类")
    @PostMapping("/update")
    @RequiresPermissions("category:update")
    public R update(@RequestBody CategoryEntity categoryEntity){
        ValidatorUtils.validateEntity(categoryEntity);
        categoryService.updateById(categoryEntity);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除商品分类")
    @PostMapping("/delete")
    @RequiresPermissions("category:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Long[] ids){
        List<CategoryEntity> categoryList = categoryService.list(
                new QueryWrapper<CategoryEntity>().in("parent_id",ids)
        );
        if (!StringUtil.isNullOrEmpty(categoryList)) {
            return R.error("请先删除子分类");
        }
        for (Long id : ids) {
            CategoryEntity category = categoryService.getById(id);
            try {
                sysOssService.deleteByUrl(category.getBannerUrl());
                sysOssService.deleteByUrl(category.getIconUrl());
                sysOssService.deleteByUrl(category.getImgUrl());
                sysOssService.deleteByUrl(category.getWapBannerUrl());
            } catch (Exception e) {
                new RRException(e.getMessage());
            }
            categoryService.removeById(id);
        }
        return R.ok();
    }
}
